package org.jeecg.modules.rule.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.rule.entity.OpersscFo;
import org.jeecg.modules.rule.entity.OpersscTabdetail;
import org.jeecg.modules.rule.mapper.OpersscFoMapper;
import org.jeecg.modules.rule.mapper.OpersscTabdetailMapper;
import org.jeecg.modules.rule.service.IOpersscFoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

/**
 * @Description: 手工脱敏表
 * @Author: jeecg-boot
 * @Date:   2019-06-17
 * @Version: V1.0
 */
@Service
public class OpersscFoServiceImpl extends ServiceImpl<OpersscFoMapper, OpersscFo> implements IOpersscFoService {

	@Autowired
	private OpersscFoMapper OpersscFoMapper;
	@Autowired
	private OpersscTabdetailMapper opersscTabdetailMapper;
	
	@Override
	@Transactional
	public void saveMain(OpersscFo OpersscFo, List<OpersscTabdetail> opersscTabdetailList) {
		OpersscFoMapper.insert(OpersscFo);
		for(OpersscTabdetail entity:opersscTabdetailList) {
			//外键设置
			entity.setFdSscid(OpersscFo.getId());
			opersscTabdetailMapper.insert(entity);
		}
	}

	@Override
	@Transactional
	public void updateMain(OpersscFo OpersscFo,List<OpersscTabdetail> opersscTabdetailList) {
		OpersscFoMapper.updateById(OpersscFo);
		
		//1.先删除子表数据
		opersscTabdetailMapper.deleteByMainId(OpersscFo.getId());
		
		//2.子表数据重新插入
		for(OpersscTabdetail entity:opersscTabdetailList) {
			//外键设置
			entity.setFdSscid(OpersscFo.getId());
			opersscTabdetailMapper.insert(entity);
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		opersscTabdetailMapper.deleteByMainId(id);
		OpersscFoMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			opersscTabdetailMapper.deleteByMainId(id.toString());
			OpersscFoMapper.deleteById(id);
		}
	}
	
}
